PYTHON CRASH COURSE: AN INTRODUCTION GUIDE WITH FUNDAMENTALS OF COMPUTER SCIENCE FOR TOTAL BEGINNERS WITH HANDS-ON PROJECTS, TRICKS AND TIPS TO LEARN FAST CODING CONCEPTS, TECHNIQUES AND TOOLS by TACKE JOHN & MATTHES ADRIENNE HAWKES

PYTHON CRASH COURSE: AN INTRODUCTION GUIDE WITH FUNDAMENTALS OF COMPUTER SCIENCE FOR TOTAL BEGINNERS WITH HANDS-ON PROJECTS, TRICKS AND TIPS TO LEARN FAST CODING CONCEPTS, TECHNIQUES AND TOOLS by TACKE JOHN & MATTHES ADRIENNE HAWKES

Author:TACKE, JOHN & MATTHES, ADRIENNE HAWKES [TACKE, JOHN]
Language: eng
Format: epub
Published: 2020-07-27T16:00:00+00:00


Instance variables are created within a method of the class, and they must be created with self. They must be accessed using the name of the instance, not the class, and altering the value of an instance variable will only impact that specific instance.

Class Methods and Static Methods

Since we now know how to create classes and attributes, we can discuss how to create “methods.” Methods are basically functions that belong to a class. They are used to carry out specific tasks, but they can only be used directly on an object that has been created using the class blueprint. Let’s try adding a method to the class we created above that can be used to print out the name, email, and role of an instance, all with one method. We define a method in a class much like how we define functions, with the def keyword and then the arguments the method takes. Similar to the init method, every method within a class will take the instance as the first argument, so we need to pass self first.

class Employee:

location = "Seattle, WA"

def __init__(self, name, email, role):

self.name = name

self.email = email

self.role = role

def get_info(self):

return '{} {} {}'.format(self.name, self.email, self.role)

Creating a method like this will enable us to get all the relevant info just by calling the method on our object instance of the class in a print statement. Note that we’re returning the info in the code above, so when we choose to print out the info, we need to wrap the method call in a print statement.

employee_1 = Employee("E. Davis", "[email protected]", "Hiring Manager")

employee_2 = Employee("D. Wong", "[email protected]", "Developer")

print(employee_1.get_info())

We can also use an alternate way of calling our method, calling it directly on the class, and then passing in the desired instance.

print(Employee.get_info(employee_1))

Much like how there is more than one kind of variables when it comes to classes (class and instance variables), there are also more than one kind of class methods. There are regular (instance) methods, class methods, and static methods.

Python also makes use of class and static methods. In all likelihood, you won't use either class or static methods anywhere near as often as you use the regular, instance methods. In order to create the class and static methods, we use what is referred to as a decorator. The decorator starts with an @ symbol, like this - @staticmethod or @classmethod. It is placed directly above the line containing the def keyword that starts the method.

Class methods take in classes as the first parameter instead of the self keyword, and this is usually represented by cls. The keyword cls is used to specify an entire class rather than an instance. If an instance (regular) method accesses instance variables, you might be able to guess that the primary use of class methods is to access and manipulate the class variables you have created within a class. For example, using a class method, we can change the location class variable we created above.

Below, we’ll create a class method that operates on the class.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.